<template>
  <slot v-if="!loading"></slot>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import { useUserStore } from '@/store/user';
import { showToast } from '@/utils/prompt';
import { hasPermission, gotoLogin } from '@/utils/permission';
import { getEnvValue } from '@/utils/env';
import { navigateTo, redirectNameTo } from '@/utils/utils';

// 用于复制组件名
// name: InterceptContainer
const pageGlobalInterpect = getEnvValue('VITE_PAGE_INTERCEPT');
const props = defineProps({
  showLoading: {
    type: Boolean,
    default: false,
  },
  loadingText: {
    type: String,
    default: '加载中',
  },
  intercept: {
    type: Boolean,
    default: true,
  },
  needLogin: {
    type: Boolean,
    default: false,
  },
  backUrl: {
    type: String,
    default: '/pages/home/home',
  },
});
const loading = ref(true);
// 如果无需拦截
if (pageGlobalInterpect === 'false' || !props.intercept) {
  loading.value = false;
  console.log('无需拦截');
} else {
  props.showLoading &&
    uni.showLoading({
      title: props.loadingText,
    });
  const userStore = useUserStore();
  // 如果需要登录验证
  if (props.needLogin && !userStore.isLogin) {
    showToast('请先登录');
    gotoLogin(); // token不存在跳转到登录页
  } else {
    loading.value = false;
    props.showLoading && uni.hideLoading();
  }
}
</script>
<style lang="scss" scoped></style>
